package com.johnson.news.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

import android.os.Bundle;
import android.view.View;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.johnson.news.BR;
import com.johnson.news.R;
import com.johnson.news.databinding.ActivityMyAskListBinding;
import com.johnson.news.model.bean.AskmeCell;
import com.johnson.news.model.bean.AskmeData;
import com.johnson.news.model.remote.RemoteDataSource;
import com.youmbe.library.BaseBindingActivity;
import com.youmbe.library.BaseRecyclerAdapter;

public class MyAskListActivity extends BaseBindingActivity<ActivityMyAskListBinding> {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    BaseRecyclerAdapter<AskmeCell> adapter;
    @Override
    protected void init() {
        dataBinding.askList.setLayoutManager(new LinearLayoutManager(this));

        adapter = new BaseRecyclerAdapter<>(this, R.layout.adapter_askme_list, com.johnson.news.BR.data);
        dataBinding.askList.setAdapter(adapter);
        adapter.setBindingViewClickListener(BR.onClickListener, onClickListener);

        dataBinding.askList.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getData(true);
            }

            @Override
            public void onLoadMore() {
                page++;
                getData(false);
            }
        });

        getData(true);
    }

    int page = 1;
    void getData(final boolean isRefresh) {
        RemoteDataSource.getRemoteDataSource()
                .getMyComplaint(this, 2, 12, page)
                .doFinally(new Action() {
                    @Override
                    public void run() throws Exception {
                        dataBinding.askList.loadMoreComplete();
                        dataBinding.askList.refreshComplete();
                    }
                }).subscribe(new Consumer<AskmeData>() {
            @Override
            public void accept(AskmeData askmeData) throws Exception {
//                if(askmeData.getStatus().equals("103")||askmeData.getStatus().equals("104")) {
//                    dataBinding.askNoData.setVisibility(View.GONE);
//                    if(isRefresh)
//                        adapter.setData(askmeData.getResult());
//                    else
//                        adapter.addData(askmeData.getResult());
//                } else {
//                    if(adapter.getItemCount() == 0) {
//                        dataBinding.askNoData.setVisibility(View.VISIBLE);
//                    } else
//                        dataBinding.askNoData.setVisibility(View.GONE);
//                }

                if(askmeData != null) {
                    if (isRefresh)
                        adapter.setData(askmeData.getResult());
                    else
                        adapter.addData(askmeData.getResult());
                }

                if(adapter.getItemCount() == 0) {
                    dataBinding.askNoData.setVisibility(View.VISIBLE);
                } else
                    dataBinding.askNoData.setVisibility(View.GONE);
            }
        });
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(v.getId() == R.id.item) {
                Object object = v.getTag(R.id.adapter_tag_data);
                if(object != null && object instanceof AskmeCell) {
                    AskmeCell askmeCell = (AskmeCell) object;
                    Bundle bundle = new Bundle();
                    bundle.putString("itemid", askmeCell.getItemid());
                    bundle.putString("title", askmeCell.getBt());
                    startActivity(AskmeReplyDetailActivity.class, bundle);
                }
            }
        }
    };

    @Override
    public int getContentViewId() {
        return R.layout.activity_my_ask_list;
    }
}
